<?php
/**
 * 标签管理
 * 
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class TagsController extends BaseController {

	/**
	 * 展示标签
	 * @return [type] [description]
	 */
	public function index(){
		$res = array('rcode'=>1,'data'=>NULL,'person'=>NULL,'msg'=> 'OK');
		$psize = I('get.psize', $this->m_psize,'intval');
		$tag_name = I('get.tag_name',' ','trim');
		$create_time = I('get.create_time',' ','trim');

		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;

		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
		$uinfo =$MTags->index();
		$creator = $uinfo['user_id'];
		$creator_sector = $uinfo['sector_id'];
		$MSector = M('\Admin\Model\GoalsModel:Sector');
		$arrSector = $MSector->getChild($creator_sector);

		//获取权限个人
		$whereUser = array();
		$whereUser['sector_id'] = array('IN',$arrSector);
		$arrUser = M('User')->where($whereUser)->select();
		$wheres = array();
		$wheres['_string'] = '1=1';
		$where = array();
		$where['_string'] = '1=1';
		if($arrUser){
			$dlsit_user = array();
			$lengthUser = sizeof($arrUser);
			for($j=0;$j<$lengthUser;$j++){
				$dlsit_user[$j] = $arrUser[$j]['user_id'];
			}
			$where['creator'] = array('IN',$dlsit_user);
			$wheres['a.creator'] = array('IN',$dlsit_user);
		}
		if($tag_name){
			$where['tag_name'] = array('like','%'.$tag_name.'%');
			$wheres['a.tag_name'] = array('like','%'.$tag_name.'%');
		}
		if($create_time){
			$where['create_time'] = array('like','%'.$create_time.'%');
			$wheres['a.create_time'] = array('like','%'.$create_time.'%');
		}
		$cnt = $MTags->where( $where )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );

		$Dlist = M()->field('a.tag_id,a.tag_name,a.create_time')
				    ->table('customer_tag as a')
					->where( $wheres )
				  	->order("a.tag_id desc")
				 	->limit($Page->firstRow.','.$Page->listRows )
				  	->select();

		$length = sizeof($Dlist);
		for($i=0;$i<$length;$i++){
			$wheres['a.tag_id'] = $Dlist[$i]['tag_id'];
			$Dlist[$i]['person'] = M()->table('customer_tag as a')
									->join('customer_tag_list as b on b.tag_id = a.tag_id')
									->join('user as c on c.user_id = a.creator')
									->join('user as d on d.user_id = b.user_id')
									->where( $wheres )
									->group('a.tag_id')
								  	->count();
			if(!$Dlist[$i]['person']){
				$Dlist[$i]['person'] = 0;
			}
		}
		if( $Dlist ){
			$res['rcode'] = 1;
			$res['data'] = $Dlist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 0;
		}
		e_json( $res );
	}
	/**
	 * 添加标签操作
	 */
	public function add(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');

		$name = I('post.name',false,'trim');
		$addres = $MTags->add_tags($name);
		if( $addres['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $addres['d'];
		}
		else{
			$res['msg'] = $addres['i'];
		}
		e_json( $res );
	}

	/**
	 * 标签修改操作
	 * @return [type] [description]
	 */
	public function edit(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');

		$tag_id = I('post.tag_id',false,'intval');
		$tag_name = I('post.name',false,'trim');
		$addres = $MTags->update_tag($tag_id, $tag_name);
		if( $addres['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $addres['d'];
		}
		else{
			$res['msg'] = $addres['i'];
		}
		e_json( $res );
	}

	/**
	 * 标签删除操作
	 * @return [type] [description]
	 */
	public function remove(){

		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$tag_id = I('get.tag_id',false,'intval');
		$random = I('get.random',$this->m_randoom,'intval');
		if( $random ==0){

			$wheres = array();
			$wheres['a.tag_id'] = array('eq', $tag_id);
			$dlist = M()->table('customer_tag  as  a')
			  ->join('customer_tag_list  as  b  on  a.tag_id = b.tag_id')
			  ->where( $wheres )
			  ->order("a.create_time asc ")
			  ->select();

			  if($dlist){
			  	$res['rcode'] = 1;
			  }else{
			  	$res['rcode'] = 5;
			  }
		}else{
			$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
			$delres = $MTags->delete_tag( $tag_id,$random);	
			if( $delres['r']==1){
				$res['rcode'] = 3;
				$res['msg'] = L('DATA_DELETE_OK');
			}else{
				$res['rcode'] = 4;
			}
		}
		e_json( $res );
	}

	/**
	 * 标签关联客户信息
	 */
	public function tagCustomer(){

		$psize = I('get.psize', $this->m_psize,'intval');
		$tag_id = I('get.tag_id',false,'intval');
		
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;

		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');

		$wheres = array();
		$wheres['_string'] = '1=1';
		$wheres['a.tag_id'] =array("eq",$tag_id);

		$cnt = M()->field('a.tag_id,a.tag_name,a.create_time,a.creator,a.count,b.tags_id,b.user_id,b.customer_id,b.tag_id,b.create_time,c.customer_id,c.nickname')
			  ->table('customer_tag  as  a')
			  ->join('customer_tag_list  as  b  on  a.tag_id = b.tag_id')
			  ->join('customer as c on b.customer_id = c.customer_id')
			  ->where( $wheres )
			  ->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );

		$dlist = M()->field('a.tag_id,a.tag_name,a.create_time,a.creator,a.count,b.tags_id,b.user_id,b.customer_id,b.tag_id,b.create_time,c.customer_id,c.nickname,c.login_name,c.phone,c.create_time')
			  ->table('customer_tag as a')
			  ->join('customer_tag_list  as  b  on  a.tag_id = b.tag_id')
			  ->join('customer as c on b.customer_id = c.customer_id')
			  ->where( $wheres )
			  ->limit($Page->firstRow.','.$Page->listRows )
			  ->order("c.customer_id desc ")
			  ->select();

		if( $dlist ){
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 0;
		}
		e_json( $res );	
	}

	/**
	 * 关联客户删除操作
	 * @return [type] [description]
	 */
	public function removeCustomer(){

		$tags_id = I('get.id',false,'intval');
		$tag_id = I('get.tag_id',false,'intval');

		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		if( !$tags_id || !$tag_id ){
			$res['msg'] = L('用户信息获取失败！');
		}else{
			$MTags = M('\Admin\Model\TagsModel:list','customer_tag_');
			$delres = $MTags->delete_customer( $tags_id,$tag_id);	
			if( $delres['r']==1){
				$res['rcode'] = 1;
			}else if( $delres['r']==2){
				$res['rcode'] = 2;
				$res['msg'] = $delres['i'];
			}
		}
		e_json( $res );
	}
}